# ========================
# T-101 Telegram Bot
#                   Server
# ========================


# Информация о системе
def getServer():

    import shutil


    # Uptime
    with open('/proc/uptime', 'r') as f:
        uptime_seconds = float(f.readline().split()[0])
    uptime = 'Uptime: ' + str(round(uptime_seconds / 60 / 60)) + ' hr.\n\n'

    # CPU load average
    cpu = '=== CPU ===\n'
    with open('/proc/loadavg', 'r') as f:
        load_avg = f.readline().split()
    cpu += ' load: ' + str(load_avg[0]) + ' / ' + str(load_avg[1]) + ' / ' + str(load_avg[2]) + '\n\n'

    # RAM free
    mem = '=== RAM ===\n'
    with open('/proc/meminfo', 'r') as f:
        mem += ' full: ' + str(round(int(f.readline().split()[1]) / 1024)) + ' Mb\n'
        for line in f:
            if 'MemFree' in line:
                mem += ' free: ' + str(round(int(line.split()[1]) / 1024)) + ' Mb\n\n'
                break

    # HDD
    hdd = '=== HDD ===\n'
    hdd_root = shutil.disk_usage("/")
    hdd += " root: %d GiB" % (hdd_root[0] // (2**30))
    hdd += " | %d GiB" % (hdd_root[1] // (2**30))
    hdd += " | %d GiB" % (hdd_root[2] // (2**30)) + '\n'
    hdd_data = shutil.disk_usage("/data")
    hdd += " data: %d GiB" % (hdd_data[0] // (2**30))
    hdd += " | %d GiB" % (hdd_data[1] // (2**30))
    hdd += " | %d GiB" % (hdd_data[2] // (2**30)) + '\n'
    hdd_opt = shutil.disk_usage("/opt")
    hdd += " opt: %d GiB" % (hdd_opt[0] // (2**30))
    hdd += " | %d GiB" % (hdd_opt[1] // (2**30))
    hdd += " | %d GiB" % (hdd_opt[2] // (2**30)) + '\n\n'

    return uptime + cpu + mem + hdd

